# Maintainer: Santiago Torres-Arias <santiago@archlinux.org>
# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>

pkgname=cosign
pkgver=2.1.1
pkgrel=1
pkgdesc="Container Signing with support for ephemeral keys and Sigstore signing"
arch=('loong64' 'x86_64')
url="https://github.com/sigstore/cosign"
license=('Apache')
depends=('pcsclite')
makedepends=('go' 'git')
checkdepends=('go-tools')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/sigstore/${pkgname}/archive/v${pkgver}.tar.gz")
sha512sums=('1c938ad9fbc48a04cbbab76cc11d8c0b34d936b7d0e092b009ace4f3086a6f135af4e7c9ed921639425baaaba7a593ea4e04dcb08bbe104da0ece64f82a16aef')
b2sums=('15a6eaf130dc57044feb8b5d8cc0d2fa8457ff68d3f1b7968567a662ea1ae7d7a62def107b9745cc8a11fcecd66f573a444ce5a64206a689740a53062f1f7bdb')
options=('!lto')

build(){
  cd "${pkgname}-${pkgver}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_ENABLED=1
  export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external -ldflags=-buildid='' -ldflags=-X=sigs.k8s.io/release-utils/version.gitVersion=v$pkgver"
  mkdir build
  go build -o build/ ./cmd/cosign
}

# Disabling tests, because of a segfault
#check() {
#  cd "${pkgname}-${pkgver}"
#  GODEBUG=x509sha1=1 make test # we have to allow sha1 certificates for tests
#}

package() {
  cd "${pkgname}-${pkgver}"
  install -Dm755 build/cosign "${pkgdir}/usr/bin/${pkgname}"

  # build bash completions
  mkdir -p "${pkgdir}/usr/share/bash-completion/completions"
  "$pkgdir"/usr/bin/cosign completion bash > "${pkgdir}/usr/share/bash-completion/completions/cosign"

  # build zsh completions
  mkdir -p "${pkgdir}/usr/share/zsh/site-functions"
  "$pkgdir"/usr/bin/cosign completion zsh > "${pkgdir}/usr/share/zsh/site-functions/_cosign"

  # build fish completions
  mkdir -p "${pkgdir}/usr/share/fish/vendor_completions.d/"
  "$pkgdir"/usr/bin/cosign completion fish > "${pkgdir}/usr/share/fish/vendor_completions.d/cosign.fish"
}

